package com.example.myapplication.fragment

import android.util.Log
import android.view.View
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.navigation.fragment.findNavController
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.example.myapplication.MainViewModel
import com.example.myapplication.R
import com.example.myapplication.databinding.FragmentMyApplyRewardBinding
import com.example.myapplication.databinding.RewardItemLayoutBinding
import com.example.myapplication.db.ApplyUser
import com.example.myapplication.db.Reward
import com.example.myapplication.utils.TimeUtils
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken


class MyApplyRewardFragment : BaseFragment<FragmentMyApplyRewardBinding>() {
    private val mViewModel: MainViewModel by activityViewModels()

    override fun initBinding(): FragmentMyApplyRewardBinding {
        return FragmentMyApplyRewardBinding.inflate(layoutInflater)
    }

    override fun initView() {
        val gson= Gson()
        val timeUtils=TimeUtils()
        var thisAccount=""
        mViewModel.Logged_user.observe(viewLifecycleOwner, Observer{
            thisAccount=it
            mBinding.myApplyRecycleView.models=mViewModel.getRewardApplyByUser(thisAccount)

        })
        mBinding.myApplyRecycleView.layoutManager = LinearLayoutManager(context)

        mBinding.myApplyRecycleView.linear().setup {
            addType<Reward>(R.layout.reward_item_layout)
            onBind {
                val binding = getBinding<RewardItemLayoutBinding>()
                val model = getModel<Reward>()
                Glide.with(context)
                    .load(R.drawable.user)
                    .into(binding.ivIcon)
                binding.tvTitle.text = model.account
                binding.tvCountry.text = model.content
                binding.time.text=timeUtils.splitDateTime(model.time)
                binding.state.visibility= View.VISIBLE

                val listType = object : TypeToken<List<ApplyUser>>() {}.type
                val applyUsers: List<ApplyUser> = gson.fromJson(model.rewardApplyAccountList, listType)
                var thisUserApplyState = ""
                applyUsers.forEach{
                    if(it.account==thisAccount){
                        when(it.state){
                            0->{ thisUserApplyState="已婉拒" }
                            1->{thisUserApplyState="正在申请"}
                            2->{thisUserApplyState="已同意，正在解决"}
                            3->{thisUserApplyState="已解决"}
                        }
                        return@forEach
                    }
                }
                binding.state.text=thisUserApplyState
                binding.price.text=model.price.toString()+"￥"
                itemView.setOnClickListener {
                    mViewModel.findUserByAccount(thisAccount).also {
                        mViewModel.thisRewardContext.value=model
                    }
                    findNavController().navigate(R.id.action_myApplyRewardFragment_to_rewardFragment)
                }
            }
        }
        mBinding.back.setOnClickListener {
            findNavController().popBackStack()
        }
    }

}